package com.jianggy.encryption;

import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

/**
 *3DES加密解密
 *@author jianggy
 *@date 2022/5/19
 *@version 1.0
 **/
public class ThreeDesTest {
    public static void main(String[] args) throws Exception {
        String password = "keN6/WIsLKFiGjd1lDhYJsQIDlg+mGeY";
        String src = "insightone123456";
        // DESedeKeySpec desedeKeySpec = new DESedeKeySpec(Base64.getEncoder().encode(password.getBytes()));
        // SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
        // Key convertSecretKey = factory.generateSecret(desedeKeySpec);
        // //加密
        // Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        // cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        //
        // byte[] result = cipher.doFinal(src.getBytes());
        // System.out.println("jdk 3des encrypt : " + Base64.getEncoder().encodeToString(result));
        // //解密
        // cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        // result = cipher.doFinal(result);
        // System.out.println("jdk 3des decrypt : " + new String(result));

        encrypt(password, src);
        descrypt(password, "VfDz2UWmGTWnUK3+EJ4DD2XoVnJbjAP4");
    }

    /**
     * 加密
     * @param key 密钥
     * @param content 明文，需要被加密的内容
     * @return
     * @throws Exception
     */
    public static String encrypt(String key,String content) throws Exception {
        DESedeKeySpec desedeKeySpec = new DESedeKeySpec(Base64.getEncoder().encode(key.getBytes()));
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
        Key convertSecretKey = factory.generateSecret(desedeKeySpec);
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(content.getBytes());
        System.out.println("jdk 3des encrypt : " + Base64.getEncoder().encodeToString(result));
        return Base64.getEncoder().encodeToString(result);
    }

    /**
     * 解密
     * @param key 密钥
     * @param encryptContent 加密密文
     * @return
     * @throws Exception
     */
    public static String descrypt(String key,String encryptContent) throws Exception{
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        DESedeKeySpec desedeKeySpec = new DESedeKeySpec(Base64.getEncoder().encode(key.getBytes()));
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
        Key convertSecretKey = factory.generateSecret(desedeKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(Base64.getDecoder().decode(encryptContent));
        System.out.println("jdk 3des decrypt : " + new String(result));
        return new String(result);
    }

    public static String generateKey() throws Exception {
        //生成KEY
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");
        keyGenerator.init(168);
        keyGenerator.init(new SecureRandom());//默认长度
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();
        String keyStr = Base64.getEncoder().encodeToString(bytesKey);
        System.out.println(keyStr);
        return keyStr;
    }

}
